package com.lw.vue_shop_server.controller;

import com.lw.vue_shop_server.po.Role;
import com.lw.vue_shop_server.service.impl.RoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class RoleController {

    @Autowired
    public RoleServiceImpl roleService;

    @GetMapping("/roles")
    public Map getRoleList(){
        return roleService.getRoleList();
    }

    @GetMapping("/roles/{id}")
    public Map findRoleById(@PathVariable(value = "id")Integer id){
        return roleService.findRoleById(id);
    }

    @PutMapping("/roles/{id}")
    public Map updateRoleById(@PathVariable(value = "id")Integer id, HttpServletRequest request){
        Role role = new Role();
        role.setId(id);
        role.setRoleName(request.getParameter("roleName"));
        role.setRoleDesc(request.getParameter("roleDesc"));
        return  roleService.updateRoleById(role);
    }

    @DeleteMapping("/roles/{id}")
    public Map delRoleById(@PathVariable(value = "id")Integer id){
        return roleService.delRoleById(id);
    }

    @PostMapping("roles")
    public Map addRole(HttpServletRequest request){
        Role role = new Role();
        role.setRoleName(request.getParameter("roleName"));
        role.setRoleDesc(request.getParameter("roleDesc"));
        return roleService.addRole(role);
    }
}
